package home.my.recordmanage.web;

import home.my.recordmanage.model.Style;
import home.my.recordmanage.service.StyleService;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class StyleServlet extends HttpServlet {
	private static final long serialVersionUID = -4493213423602314472L;
	ApplicationContext context;
	ServletContext application;
	List<String> styleList;
	String style;
	Logger log = Logger.getLogger(StyleServlet.class);

	@Override
	public void init(ServletConfig config) throws ServletException {
		context = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
		application = config.getServletContext();
		initStyleList();
		initCurrentStyle();
		
		//log
		StringBuilder sb = new StringBuilder();
		for(String s:styleList){
			sb.append(s).append(",");
		}
		sb.delete(sb.length()-1, sb.length());
		log.warn("可用样式有：" + sb.toString());
		log.warn("当前样式：" + style);
	}


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		initStyleList();
		String changeStyle = req.getParameter("changeStyle");
		if( StringUtils.isNotEmpty(changeStyle) && styleList.contains(changeStyle) ){
			saveCurrentStyleToDb(changeStyle);
			application.setAttribute("style", changeStyle);
		}
		resp.getWriter().write("{}");
		resp.getWriter().flush();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req,resp);
	}
	
	//列文件列表获取已经下载到的jqueryui皮肤，放入application
	private void initStyleList(){
		StyleService styleService = (StyleService)context.getBean("styleService");
		styleList = styleService.listAllStylesFromFileSystem(application.getRealPath("/"));
		application.setAttribute("styleList", styleList);
	}
	
	//从数据库取出当前用的style，放入application
	private void initCurrentStyle() {
		StyleService styleService = (StyleService)context.getBean("styleService");
		List<Style> dbStyleList = styleService.find("id", false);
		if(dbStyleList!=null && dbStyleList.size()>0){
			style = dbStyleList.get(0).getName();
			if(!styleList.contains(style)){
				style = styleList.get(0);
				saveCurrentStyleToDb(style);
			}
		}else if(styleList!=null && styleList.size()>0){
			style = styleList.get(0);
		}else{
			style = "";
		}
		application.setAttribute("style", style);
	}
	
	private void saveCurrentStyleToDb(String styleName){
		StyleService styleService = (StyleService)context.getBean("styleService");
		styleService.saveCurrentStyleToDb(styleName);
	}
}
